var db = require('../model');
var utils = require('../utils');

module.exports = {
  all: function(req, res, next) {
    var paging = utils.getPaging(req.query);
    db.user.find({}).sort({
      created_at: - 1
    }).select('-_id -pwd').skip(paging.skip).limit(paging.count).then(ret => {
      res.json(utils.formatResult(ret));
    },
    err => {
      next(err);
    });
  },
  info: function(req,res,next) {
    var id = req.query.id;
    db.user.findOne({
      id:id
    }).select('-_id -pwd').lean().then(ret=>{
      utils.fixUser([ret]);
      res.json(utils.formatResult(ret)); 
    },next);
  },
  update:function(req,res,next){
    var _id = req.body.id,
    nick = req.body.nick,
    profile = req.body.profile,
    about = req.body.about;
    db.user.findOneAndUpdate({
      _id:_id
    },{
      nick:nick,
      profile:profile,
      about:about
    }).select('-_id -pwd').then((ret)=>{
      res.json(utils.formatResult({status:'success'}));
    });
  }
}

